<template>
  <div id="manage-order-list">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 输入框 -->
      <el-row>
        <el-col :span="6">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表 -->
      <el-table
        :data="orderList"
        border
        stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否付款" v-slot="data">
          <el-tag v-if="data.row.pay_status === '1'" type="success">已付款</el-tag>
          <el-tag v-else type="danger">未付款</el-tag>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>
        <el-table-column label="下单事件" v-slot="data">
          {{ data.row.create_time | dateFormat }}
        </el-table-column>
        <el-table-column label="操作" v-slot="data">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="openEditAdressDialog"></el-button>
          <el-button
            size="mini"
            type="success"
            icon="el-icon-location"
            @click="openLogisticsDialog(data.row.order_id)"></el-button>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="this.queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total">
      </el-pagination>
    </el-card>
    <!-- 编辑对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="editAdressDialogVisible"
      width="50%"
      @close="handleEditDialogClose">
      <el-form
        :model="editAdressForm"
        label-position="left"
        label-width="90px"
        ref="editAdressFormRef"
        :rules="editAdressFormRules">
        <el-form-item label="省市区/县: " prop="adress1">
          <el-cascader
            v-model="editAdressForm.adress1"
            :options="editAdressCascader.cityData"
            :props="editAdressCascader.props"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址: " prop="adress2">
          <el-input
            v-model="editAdressForm.adress2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editAdressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditAdress">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流事件 -->
    <el-dialog
      title="物流进度"
      :visible.sync="logisticsDialogVisible"
      width="50%">
      <el-timeline>
        <el-timeline-item
          v-for="(value, index) in logisticsList"
          :key="index"
          :timestamp="value.time">
          {{value.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from 'common/citydata.js'

export default {
  name: 'ManageOrderList.vue',
  data () {
    return {
      // 1. 查询参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 2. 列表总数量
      total: null,
      // 订单列表
      orderList: [],
      // 控制显示editDialog
      editAdressDialogVisible: false,
      // 修改地址表单
      editAdressForm: {
        adress1: [],
        adress2: ''
      },
      // 修改地址规则
      editAdressFormRules: {
        adress1: [
          { required: true, message: '请选择地址', trigger: 'blur' }
        ],
        adress2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      // 修改城市级联表数据
      editAdressCascader: {
        cityData: cityData,
        props: {
          children: 'children',
          label: 'value',
          value: 'value'
        }
      },
      // 控制显示物流进度
      logisticsDialogVisible: false,
      // 物流信息
      logisticsList: []
    }
  },
  created () {
    this.getOrderList()
  },
  methods: {
    // 1. 获取订单列表
    async getOrderList () {
      const { data: res } = await this.$http.get('/orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表出错')
      }
      console.log(res)
      // 获取成功
      this.orderList = res.data.goods
      // 返回总数
      this.total = res.data.total
    },
    // 2. 打开编辑地址对话框
    openEditAdressDialog () {
      this.editAdressDialogVisible = true
    },
    // 3. editDialog关闭的时候触发
    handleEditDialogClose () {
      console.log(this.editAdressForm)
    },
    // 4. 确认修改地址
    submitEditAdress () {
      console.log('修改地址完成')
      this.editAdressDialogVisible = false
    },
    // 5. 打开物流信息对话框
    async openLogisticsDialog (id) {
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) {
        return this.$message.error('获取物流信息失败')
      }
      console.log(res)
      // 获取成功
      this.logisticsList = res.data
      this.logisticsDialogVisible = true
    },
    handleSizeChange (pagesize) {
      this.queryInfo.pagesize = pagesize
      this.getOrderList()
    },
    handleCurrentChange (pagenum) {
      this.queryInfo.pagenum = pagenum
      this.getOrderList()
    }
  }
}
</script>

<style scoped lang="less">
#manage-order-list {
  .el-breadcrumb {
    font-size: 12px;
  }
  .el-card {
    margin-top: 15px;
  }
  .el-table {
    margin-top: 15px;
    font-size: 12px;
  }
  .el-pagination {
    margin-top: 15px;
  }
}
</style>
